package manager.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import manager.dao.proxy.UserDAOProxy;
import manager.vo.User;
import cn.tool.Md5;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User>{
	private static final long serialVersionUID = 1L;
	public User user = new User();
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return this.user;
	}
	public String returnMsg = new String();	
	public String getReturnMsg() {
		return returnMsg;
	}
	public void setReturnMsg(String returnMsg) {
		this.returnMsg = returnMsg;
	}
	//登陆验证
	public String login() throws Exception{
		List<User> list = UserDAOProxy.getInstance().checkLogin(user);
	    if(list.size()>0){
	    	ServletActionContext.getRequest().getSession().setAttribute("user", list.get(0));
	    	this.returnMsg = "1";
	    }else{
	    	this.returnMsg = "0";
	    }
	    return "returnMsg";
	}
	public String checkUserName() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("checkName", user.getName());
		List<User> list = UserDAOProxy.getInstance().selectUser(map);
		if(list.size()>0){
	    	this.returnMsg = "1";
		}else{
		    this.returnMsg = "0";
		}
	    return "returnMsg";	
	}
	public String regUser() throws Exception{
		this.user.setIdentity("0");
		this.user.setReg_date(new java.sql.Date(new Date().getTime()));
		if(UserDAOProxy.getInstance().addUser(user)){
			this.returnMsg =  "1";
		}else{
			this.returnMsg = "0";
		}
		return "returnMsg";
	}
	public String checkUserid() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userid", user.getUserid());
		if(UserDAOProxy.getInstance().selectUser(map).size()>0){
			this.returnMsg = "1";
		}else{
			this.returnMsg = "0";
		}
		return "returnMsg";
	}
	public String checkSession() throws Exception{
		if(ServletActionContext.getRequest().getSession().getAttribute("user")==null){
			this.returnMsg = "0";
		}else{
			this.returnMsg = "1";
		}
		return "returnMsg";
	}
	//取用户名
	public String getUserName() throws Exception {
		User user = (User)ServletActionContext.getRequest().getSession().getAttribute("user");
		if(user!=null){
			this.returnMsg = user.getName();
		}else{
			this.returnMsg = "0";
		}
		return "returnMsg";
	}
	//销毁session
	public String exit() throws Exception{
		ServletActionContext.getRequest().getSession().invalidate();
		this.returnMsg = "1";
		return "returnMsg";
	}
	
}
